package com.itboye.hutouben.activity.store.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.LinearLayout;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.OrderAdapter;
import com.itboye.hutouben.base.BaseFragment;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.DingDanQuery;
import com.itboye.hutouben.presenter.DingDanPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.MyListView;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 我的订单
 */
@SuppressLint("ValidFragment")
public class OrderFragment extends BaseFragment implements Observer {
    private int type;
    MyListView order_listView;
    OrderAdapter adapter;
    private LinearLayout linearOrder;

    DingDanPresenter dingDanPresenter;
    String uid;
    DingDanQuery bean;
    List<DingDanQuery.ListEntity> beanList = new ArrayList<>();
    PtrFrameLayout commend_anchor_ptr;
    int pageIndex = 1;
    public static OrderFragment act;

    public OrderFragment() {
    }
    public OrderFragment(int type) {
        this.type = type;
    }

    @Override
    public int initView() {
        return R.layout.fragment_order;
    }

    @Override
    public void onMyActivityCreated() {
        uid = (String) SPUtils.get(getActivity(), null, Const.ID, "");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        beanList=new ArrayList<DingDanQuery.ListEntity>();
        dingDanPresenter = new DingDanPresenter(this);
        showProgressDialog("数据加载中...", true);
        dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
        adapter = new OrderAdapter(beanList, getActivity(), type);
        order_listView.setAdapter(adapter);
        act=this;
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {//刷新的监听事件
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
              //  pageIndex = 1;
                dingDanPresenter.orderQuery(IsUtilUid.sId(), uid, "", type + "", pageIndex + "", "10");
            }
        });
       // commend_anchor_ptr.autoRefresh(true);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void update(Observable observable, Object data) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(data);
        commend_anchor_ptr.refreshComplete();
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == DingDanPresenter.daingdan_query_success) {
                if (pageIndex==1) {
                    beanList.clear();
                }
                bean = (DingDanQuery) entity.getData();
                beanList.addAll(bean.getList());
                if (beanList.size() > 0) {
                    adapter.notifyDataSetChanged();
                    order_listView.setVisibility(View.VISIBLE);
                    linearOrder.setVisibility(View.GONE);
                } else {
                    order_listView.setVisibility(View.GONE);
                    linearOrder.setVisibility(View.VISIBLE);
                }
            }
            if (entity.getEventType() == DingDanPresenter.daingdan_query_fail) {
                ByAlert.alert(entity.getData());
            }
        }

    }

}
